#include "StdAfx.h"
#include <string>
#include <Util/StringUtil.h>
#include <interface/data_op/table_wrapper.h>
#include "login_info_op.h"
#include <commondef/glb_define.h>

namespace aot{ namespace data{

login_info_op_impl::login_info_op_impl(void)
{

}

login_info_op_impl::~login_info_op_impl(void)
{

}

bool 
login_info_op_impl::del_all()
{
    std::string sql_exe = "delete from login_info;";
    return g_db_cfg->exec(sql_exe.c_str());
}

bool 
login_info_op_impl::insert_latest_login_info(ilogin_info* p)
{
    std::string str_user_id = p->user_id->c_str();
    std::string str_user_def = p->user_def->c_str();
    std::string str_email = p->email->c_str();
    std::string str_mobile = p->mobile->c_str();
    std::string str_ent_name = p->ent_name->c_str();
    std::string str_ent_phone = p->ent_phone->c_str();
    std::string str_oa_user = p->oa_user->c_str();
    std::string str_password = p->password->c_str();
    std::string str_create_time = p->create_time->c_str();
    std::string str_last_login_time = p->last_login_time->c_str();
    
   
    remove_sin_quot(str_user_id);
    remove_sin_quot(str_user_def);
    remove_sin_quot(str_email);
    remove_sin_quot(str_mobile);
    remove_sin_quot(str_ent_name);
    remove_sin_quot(str_ent_phone);
    remove_sin_quot(str_oa_user);
    remove_sin_quot(str_password);
    remove_sin_quot(str_create_time);
    remove_sin_quot(str_last_login_time);
   

    std::string sql_fmt = 
        "insert into login_info \
       (user_id, \
        user_def, \
        email, \
        mobile, \
        ent_name, \
        ent_phone, \
        oa_user, \
        password, \
        create_time, \
        last_login_time, \
        login_type, \
        auto_login, \
        face_image, \
        login_status, \
        save_info, \
        save_password) \
        values\
        ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s', \
        %d, %d, %d, %d, %d, %d);";

    std::string sql_exe;

    sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(),
        str_user_id.c_str(),        
        str_user_def.c_str(),      
        str_email.c_str(),        
        str_mobile.c_str(),       
        str_ent_name.c_str(),      
        str_ent_phone.c_str(), 
        str_oa_user.c_str(),       
        str_password.c_str(), 
        str_create_time.c_str(),
        str_last_login_time.c_str(),
        p->login_type,
        p->auto_login,
        p->face_image,
        p->login_status,
        p->save_info,
        p->save_password
        );


    return g_db_cfg->exec(sql_exe.c_str());
}

bool 
login_info_op_impl::del_latest_login_info(const char* user_id)
{
    std::string sql_fmt = "delete from login_info where user_id='%s';";
    std::string sql_exe;
    sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(), user_id);
    return g_db_cfg->exec(sql_exe.c_str());
}

bool 
login_info_op_impl::get_latest_login_info(const char* user_id, ilogin_info** out)
{
	*out = new login_info_impl;
    return this->get_latest_login_info(user_id, *out);
}

bool 
login_info_op_impl::get_latest_login_info(const char* user_id, ilogin_info* out)
{
    bool ret = false;
    std::string sql_fmt = "select \
                          user_id, \
                          user_def, \
                          email, \
                          mobile, \
                          ent_name, \
                          ent_phone, \
                          oa_user, \
                          password, \
                          create_time, \
                          last_login_time, \
                          login_type, \
                          auto_login, \
                          face_image, \
                          login_status, \
                          save_info, \
                          save_password \
                          from login_info \
                          where user_id='%s';";
    std::string sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(), user_id);
   
    aot::data::table_helper t_table;
    if ( g_db_cfg->exec_and_get_record(sql_exe.c_str(), t_table) )
    {
        
      if  ( t_table.get_rows_count() > 0 )
      {
          t_table.set_row_index(0);

          out->user_id->assign(t_table.get_field_value_str("user_id").c_str());
          out->user_def->assign(t_table.get_field_value_str("user_def").c_str());
          out->email->assign(t_table.get_field_value_str("email").c_str());
          out->mobile->assign(t_table.get_field_value_str("mobile").c_str());
          out->ent_name->assign(t_table.get_field_value_str("ent_name").c_str());
          out->ent_phone->assign(t_table.get_field_value_str("ent_phone").c_str());
          out->oa_user->assign(t_table.get_field_value_str("oa_user").c_str());
          out->password->assign(t_table.get_field_value_str("password").c_str());
          out->create_time->assign(t_table.get_field_value_str("create_time").c_str());
          out->last_login_time->assign(t_table.get_field_value_str("last_login_time").c_str());

          out->login_type = t_table.get_field_value_int("login_type");
          out->auto_login = t_table.get_field_value_int("auto_login");
          out->face_image = t_table.get_field_value_int("face_image");
          out->login_status = t_table.get_field_value_int("login_status");
          out->save_info = t_table.get_field_value_int("save_info");
          out->save_password = t_table.get_field_value_int("save_password");    
          ret = true;
      }
    }
    return ret;
}

bool login_info_op_impl::get_latest_login_info( ilogin_info** out )
{

	std::string sql_exe = "select \
						  user_id, \
						  user_def, \
						  email, \
						  mobile, \
						  ent_name, \
						  ent_phone, \
						  oa_user, \
						  password, \
						  create_time, \
						  last_login_time, \
						  login_type, \
						  auto_login, \
						  face_image, \
						  login_status, \
						  save_info, \
						  save_password \
						  from login_info \
						  order by last_login_time desc;";
	aot::data::table_helper t_table;
	bool ret = g_db_cfg->exec_and_get_record(sql_exe.c_str(), t_table);
	if ( ret )
	{
		if ( t_table.get_rows_count() <= 0 )
		{
			return false;
		}

		t_table.set_row_index(0);

		ilogin_info* new_login = new login_info_impl;
		new_login->user_id->assign(t_table.get_field_value_str("user_id").c_str());
		new_login->user_def->assign(t_table.get_field_value_str("user_def").c_str());
		new_login->email->assign(t_table.get_field_value_str("email").c_str());
		new_login->mobile->assign(t_table.get_field_value_str("mobile").c_str());
		new_login->ent_name->assign(t_table.get_field_value_str("ent_name").c_str());
		new_login->ent_phone->assign(t_table.get_field_value_str("ent_phone").c_str());
		new_login->oa_user->assign(t_table.get_field_value_str("oa_user").c_str());
		new_login->password->assign(t_table.get_field_value_str("password").c_str());
		new_login->create_time->assign(t_table.get_field_value_str("create_time").c_str());
		new_login->last_login_time->assign(t_table.get_field_value_str("last_login_time").c_str());

		new_login->login_type = t_table.get_field_value_int("login_type");
		new_login->auto_login = t_table.get_field_value_int("auto_login");
		new_login->face_image = t_table.get_field_value_int("face_image");
		new_login->login_status = t_table.get_field_value_int("login_status");
		new_login->save_info = t_table.get_field_value_int("save_info");
		new_login->save_password = t_table.get_field_value_int("save_password");
		*out = new_login;
	}
	return ret;
}

bool 
login_info_op_impl::get_latest_login_info_list(ilogin_info_list** out)
{
    std::string sql_exe = "select \
                          user_id, \
                          user_def, \
                          email, \
                          mobile, \
                          ent_name, \
                          ent_phone, \
                          oa_user, \
                          password, \
                          create_time, \
                          last_login_time, \
                          login_type, \
                          auto_login, \
                          face_image, \
                          login_status, \
                          save_info, \
                          save_password \
                          from login_info \
                          order by last_login_time desc;";
    aot::data::table_helper t_table;
    bool ret = g_db_cfg->exec_and_get_record(sql_exe.c_str(), t_table);
    if ( ret )
    {
        login_info_list* new_list = new login_info_list();
        *out = new_list;
        for( int i=0; i<t_table.get_rows_count(); i++ )
        {
            t_table.set_row_index(i);

            login_info_impl* new_login = new login_info_impl();

            new_login->user_id->assign(t_table.get_field_value_str("user_id").c_str());
            new_login->user_def->assign(t_table.get_field_value_str("user_def").c_str());
            new_login->email->assign(t_table.get_field_value_str("email").c_str());
            new_login->mobile->assign(t_table.get_field_value_str("mobile").c_str());
            new_login->ent_name->assign(t_table.get_field_value_str("ent_name").c_str());
            new_login->ent_phone->assign(t_table.get_field_value_str("ent_phone").c_str());
            new_login->oa_user->assign(t_table.get_field_value_str("oa_user").c_str());
            new_login->password->assign(t_table.get_field_value_str("password").c_str());
            new_login->create_time->assign(t_table.get_field_value_str("create_time").c_str());
            new_login->last_login_time->assign(t_table.get_field_value_str("last_login_time").c_str());

            new_login->login_type = t_table.get_field_value_int("login_type");
            new_login->auto_login = t_table.get_field_value_int("auto_login");
            new_login->face_image = t_table.get_field_value_int("face_image");
            new_login->login_status = t_table.get_field_value_int("login_status");
            new_login->save_info = t_table.get_field_value_int("save_info");
            new_login->save_password = t_table.get_field_value_int("save_password");

            new_list->impl().push_back(new_login);
        }
    }
    return ret;
}

bool login_info_op_impl::get_latest_login_info_list( int login_type, ilogin_info_list** out )
{
	std::string sql_fmt = "select \
						  user_id, \
						  user_def, \
						  email, \
						  mobile, \
						  ent_name, \
						  ent_phone, \
						  oa_user, \
						  password, \
						  create_time, \
						  last_login_time, \
						  login_type, \
						  auto_login, \
						  face_image, \
						  login_status, \
						  save_info, \
						  save_password \
						  from login_info where login_type = %d \
						  order by last_login_time desc;";

	std::string sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(), login_type);
	aot::data::table_helper t_table;
	bool ret = g_db_cfg->exec_and_get_record(sql_exe.c_str(), t_table);
	if ( ret )
	{
		login_info_list* new_list = new login_info_list();
		*out = new_list;
		for( int i=0; i<t_table.get_rows_count(); i++ )
		{
			t_table.set_row_index(i);

			login_info_impl* new_login = new login_info_impl();

			new_login->user_id->assign(t_table.get_field_value_str("user_id").c_str());
			new_login->user_def->assign(t_table.get_field_value_str("user_def").c_str());
			new_login->email->assign(t_table.get_field_value_str("email").c_str());
			new_login->mobile->assign(t_table.get_field_value_str("mobile").c_str());
			new_login->ent_name->assign(t_table.get_field_value_str("ent_name").c_str());
			new_login->ent_phone->assign(t_table.get_field_value_str("ent_phone").c_str());
			new_login->oa_user->assign(t_table.get_field_value_str("oa_user").c_str());
			new_login->password->assign(t_table.get_field_value_str("password").c_str());
			new_login->create_time->assign(t_table.get_field_value_str("create_time").c_str());
			new_login->last_login_time->assign(t_table.get_field_value_str("last_login_time").c_str());

			new_login->login_type = t_table.get_field_value_int("login_type");
			new_login->auto_login = t_table.get_field_value_int("auto_login");
			new_login->face_image = t_table.get_field_value_int("face_image");
			new_login->login_status = t_table.get_field_value_int("login_status");
			new_login->save_info = t_table.get_field_value_int("save_info");
			new_login->save_password = t_table.get_field_value_int("save_password");

			new_list->impl().push_back(new_login);
		}
	}
	return ret;
}

bool 
login_info_op_impl::save_latest_login_info_list(ilogin_info_list* in)
{
    if (in->size() == 0)
    {
        return false;
    }

    this->begin_transaction(true);
    g_db_cfg->exec("delete from login_info;");

    for( in->goto_begin(); !in->is_end(); in->goto_next() )
    {
        this->insert_latest_login_info(in->get_value());
    }
    return this->commit_transaction(true);
}

bool 
login_info_op_impl::get_login_info( int login_type, const char* value, ilogin_info** out )
{
	std::string sql_fmt;
	switch( login_type )
	{
	default:
	case aot::e_login_type_tata:
		sql_fmt = "select * from login_info where user_id='%s' and login_type=%d;";
		break;
	case aot::e_login_type_userdef:
		sql_fmt = "select * from login_info where user_def='%s' and login_type=%d;";
		break;
	case aot::e_login_type_email:
		sql_fmt = "select * from login_info where email='%s' and login_type=%d;";
		break;
	case aot::e_login_type_mobile:
		sql_fmt = "select * from login_info where mobile='%s' and login_type=%d;";
		break;
	case aot::e_login_type_ent:
		sql_fmt = "select * from login_info where ent_name='%s' and login_type=%d;";
		break;
		break;
	}
	std::string sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(), value, login_type);
	
	aot::data::table_helper t_table;
	bool ret = g_db_cfg->exec_and_get_record(sql_exe.c_str(), t_table);
	if ( ret )
	{
		t_table.set_row_index(0);

		ilogin_info* new_login = new login_info_impl;
		new_login->user_id->assign(t_table.get_field_value_str("user_id").c_str());
		new_login->user_def->assign(t_table.get_field_value_str("user_def").c_str());
		new_login->email->assign(t_table.get_field_value_str("email").c_str());
		new_login->mobile->assign(t_table.get_field_value_str("mobile").c_str());
		new_login->ent_name->assign(t_table.get_field_value_str("ent_name").c_str());
		new_login->ent_phone->assign(t_table.get_field_value_str("ent_phone").c_str());
		new_login->oa_user->assign(t_table.get_field_value_str("oa_user").c_str());
		new_login->password->assign(t_table.get_field_value_str("password").c_str());
		new_login->create_time->assign(t_table.get_field_value_str("create_time").c_str());
		new_login->last_login_time->assign(t_table.get_field_value_str("last_login_time").c_str());

		new_login->login_type = t_table.get_field_value_int("login_type");
		new_login->auto_login = t_table.get_field_value_int("auto_login");
		new_login->face_image = t_table.get_field_value_int("face_image");
		new_login->login_status = t_table.get_field_value_int("login_status");
		new_login->save_info = t_table.get_field_value_int("save_info");
		new_login->save_password = t_table.get_field_value_int("save_password");
		*out = new_login;
	}
	return ret;
}

bool 
login_info_op_impl::is_user_save_pwd( const char* user_id )
{
	std::string sql_fmt = "select  *   from login_info where user_id = '%s';";
	std::string sql_exe = aot::tt::utility::StrUtil::Printf(sql_fmt.c_str(), user_id);

	aot::data::table_helper t_table;
	bool ret = g_db_cfg->exec_and_get_record(sql_exe.c_str(), t_table);
	if ( ret )
	{
		t_table.set_row_index(0);
		return t_table.get_field_value_int("save_password");
	}
	return ret;
}

bool 
login_info_op_impl::save_user_pwd( const char* user_id, const char* pwd, 
								  const char* cur_datetime, const char* nick_name)
{
	bool ret = false;
	ilogin_info* new_login = new login_info_impl();
	if ( this->get_latest_login_info(user_id, &new_login) )
	{
		new_login->save_password = 1;
		if ( this->del_latest_login_info(user_id) )
		{
			if ( this->insert_latest_login_info(new_login) )
			{
				ret = true;
			}
		}
	}
	else
	{
		new_login->user_id->assign(user_id);
		new_login->password->assign(pwd);
		new_login->user_def->assign(nick_name);
		new_login->create_time->assign(cur_datetime);
		new_login->save_password = 1;
		new_login->save_info = 1;
		new_login->login_type = 1;
		new_login->auto_login = 0;
		if ( this->insert_latest_login_info(new_login) )
		{
			ret = true;
		}

	}

	new_login->destroy();
	return ret;
}

bool 
login_info_op_impl::save_user_pwd_cancel( const char* user_id )
{
	bool ret = false;
	ilogin_info* new_login = new login_info_impl();
	if ( this->get_latest_login_info(user_id, &new_login) )
	{
		new_login->save_password = 0;
		new_login->auto_login = 0;
		if ( this->del_latest_login_info(user_id) )
		{
			if ( this->insert_latest_login_info(new_login) )
			{
				ret = true;
			}
		}
	}
	new_login->destroy();

	return ret;
}

}} /* end namespace aot/data*/

